﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;
using System.Security;
using System.Threading;
using Microsoft.Practices.Unity.InterceptionExtension;

namespace Infrastructure.CrossCutting.Security
{
    using Domain.UserManagement;
    using Microsoft.Practices.Unity;
    using Domain.UserManagement.Entities;

    public class UserManagementCallHandler : ICallHandler
    {
        private readonly IUnityContainer _container;
        /// <summary>
        /// 
        /// </summary>
        private readonly IUserRepository userRepository;
        /// <summary>
        /// 
        /// </summary>
        private readonly ICheckUserPermission checkUserPermission;

        public UserManagementCallHandler()
        {
            Order = 0;
        }
 
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            IMethodReturn methodReturn;

            var thisUser = CurrentUser.Instance;
            var currentUser = thisUser.ThisUser;

            //Check the calling method example
            //if (input.MethodBase.Name.Contains("Admin"))
            //{
            //    if (checkUserPermission.HasPermission(currentUser))
            //    {
            //        methodReturn = getNext().Invoke(input, getNext);
            //        return methodReturn;
            //    }
            //}

            //Security exception
            //throw new SecurityException();

            //methodReturn = input.CreateMethodReturn(true);


            methodReturn = getNext().Invoke(input, getNext);
            return methodReturn;
        }

        public int Order { get; set; }


    }
}
